package com.monkeyboy.demo.order;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.List;

/*
 *顺序消费
 */
public class OrderProducer {

    public static void main(String[] args) throws Exception {
        //1.创建消息生产者producer，并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group");
        //2.指定Nameserver地址
        producer.setNamesrvAddr("192.168.1.140:9876;192.168.1.141:9876");
        //3.启动producer
        producer.start();
        //构建消息集合
        List<OrderStep> orderSteps = OrderStep.buildOrders();
        //发送消息
        for (int i = 0; i < 36; i++) {
            Message message = new Message("OrderTopic", "Order", "i" + i, (i + "").getBytes());

            SendResult sendResult = producer.send(message, new MessageQueueSelector() {
                /**
                 * 这里发往不同的队列开启多个消费者消费查看控制台的规律
                 * @param mqs：队列集合
                 * @param msg：消息对象
                 * @param arg：业务标识的参数
                 * @return
                 */
                @Override
                public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                    return mqs.get(1);
                }
            }, null);

            System.out.println("发送结果：" + sendResult);
        }
        producer.shutdown();
    }

}
